package com.mulanweb.api.module.controller.admin;

import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.controller.BaseController;
import com.mulanweb.api.module.model.OperationHistory;
import com.mulanweb.api.module.model.Visa;
import com.mulanweb.api.module.model.VisaChild;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/8 0008.
 */
@ValidateFile
public class VisaController extends BaseController {

    /**
     * 搜索签证信息
     */
    @ValidateForm
    public void search() {
        SQLGenerateHelper helper = new SQLGenerateHelper("visa", "create_time desc");
        helper.addLikeCondition("country", getPara("country"));
        helper.addLikeCondition("title", getPara("title"));
        helper.addIsNullCondition("delete_time");
        Page<Visa> page = Visa.dao.paginate(getParaToInt("PageNumber"), getParaToInt("PageSize"), helper.getSqlSelect(), helper.getSqlExceptSelect(), helper.getQueryParas());
        returnPageModelJson(page);
    }

    @ValidateForm
    public void save() throws IOException {
        String title = getPara("title");
        String description = getPara("description");
        String country = getPara("country");
        Integer adminId = getSessionAttr("adminId");
        Visa visa = new Visa();
        visa.setTitle(title);
        visa.setCountry(country);
        visa.setDescription(description);
        visa.setStatusId(20L);//初始化状态：出售中20
        visa.setCreateTime(new Date());
        if (visa.save()) {
            OperationHistory operationHistory = new OperationHistory();
            //增加一条数据
            operationHistory.setTypeId(30L);
            operationHistory.setTargetId((long) visa.getId());
            operationHistory.setTableName("visa");
            operationHistory.setAdminId(adminId);
            operationHistory.setCreateTime(new Date());
            operationHistory.save();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("ReturnCode", 1);
            map.put("Data", visa);
            returnSuccessJson(map);
        } else {
            returnFailureJson();
        }
    }

    @ValidateForm
    public void update() throws IOException {
        String title = getPara("title");
        String description = getPara("description");
        String country = getPara("country");
        Integer adminId = getSessionAttr("adminId");
        Integer visaId=  getParaToInt("visaId");
        Visa visa = Visa.dao.findById(visaId);
        if( visa == null){
            returnFailureJson();
            return;
        }
        visa.setTitle(title);
        visa.setCountry(country);
        visa.setDescription(description);
        visa.setCreateTime(new Date());
        if (visa.update()) {
            OperationHistory operationHistory = new OperationHistory();
            //更新一条数据
            operationHistory.setTypeId(29L);
            operationHistory.setTargetId((long) visa.getId());
            operationHistory.setTableName("visa");
            operationHistory.setAdminId(adminId);
            operationHistory.setCreateTime(new Date());
            operationHistory.save();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("ReturnCode", 1);
            map.put("Data", visa);
            returnSuccessJson(map);
        } else {
            returnFailureJson();
        }
    }

    @ValidateForm
    public void saveChild() {
        Integer parentId = getParaToInt("parentId");
        String name = getPara("name");
        Float price = Float.parseFloat(getPara("price"));
        Float priceOld = Float.parseFloat(getPara("priceOld"));
        Integer adminId = getSessionAttr("adminId");
        Visa visa = Visa.dao.findById(parentId);
        if (visa != null && visa.getDeleteTime() == null) {
            VisaChild visaChild = new VisaChild();
            visaChild.setParentId(parentId);
            visaChild.setName(name);
            visaChild.setStatusId(20L);//初始化状态：出售中20
            visaChild.setPrice(price);
            visaChild.setPriceOld(priceOld);
            visaChild.setCreateTime(new Date());
            if (visaChild.save()) {
                OperationHistory operationHistory = new OperationHistory();
                //增加一条数据
                operationHistory.setTypeId(30L);
                operationHistory.setTargetId((long) visa.getId());
                operationHistory.setTableName("visa_child");
                operationHistory.setAdminId(adminId);
                operationHistory.setCreateTime(new Date());
                operationHistory.save();
                returnSuccessJson();
            }
        } else {
            returnFailureJson();
        }
    }

    @ValidateForm
    public void updateChild(){
        Integer visaChildId = getParaToInt("visaChildId");
        String name = getPara("name");
        Float price = Float.parseFloat(getPara("price"));
        Float priceOld = Float.parseFloat(getPara("priceOld"));
        Integer adminId = getSessionAttr("adminId");
        VisaChild visaChild = VisaChild.dao.findById(visaChildId);
        if (visaChild != null && visaChild.getDeleteTime() == null) {
            visaChild.setName(name);
            visaChild.setPrice(price);
            visaChild.setPriceOld(priceOld);
            if (visaChild.update()) {
                                OperationHistory operationHistory = new OperationHistory();
                //增加一条数据
                operationHistory.setTypeId(27L);
                operationHistory.setTargetId((long) visaChild.getId());
                operationHistory.setTableName("visa_child");
                operationHistory.setAdminId(adminId);
                operationHistory.setCreateTime(new Date());
                operationHistory.save();
                returnSuccessJson();
            }
        } else {
            returnFailureJson();
        }
    }

    @ValidateForm
    public void upperStatus()  {
        Integer visaId=  getParaToInt("visaId");
        Visa visa = Visa.dao.findById(visaId);
        if( visa == null){
            returnFailureJson();
            return;
        }
        visa.setStatusId(20L);
        if (visa.update()) {
            returnSuccessJson();
        } else {
            returnFailureJson();
        }
    }

    @ValidateForm
    public void lowerStatus()  {
        Integer visaId=  getParaToInt("visaId");
        Visa visa = Visa.dao.findById(visaId);
        if( visa == null){
            returnFailureJson();
            return;
        }
        visa.setStatusId(21L);
        if (visa.update()) {
            returnSuccessJson();
        } else {
            returnFailureJson();
        }
    }

    @ValidateForm
    public void delete() {
        Integer visaId = getParaToInt("visaId");
        Integer adminId = getSessionAttr("adminId");
        Visa visa = Visa.dao.findById(visaId);
        if (visa != null && visa.getDeleteTime() == null && adminId != null) {
            visa.setStatusId(22L);//状态改为已删除
            visa.setDeleteTime(new Date());
            if (visa.update()) {
                OperationHistory operationHistory = new OperationHistory();
                //更新一条数据
                operationHistory.setTypeId(29L);
                operationHistory.setTargetId((long) visa.getId());
                operationHistory.setTableName("visa");
                operationHistory.setAdminId(adminId);
                operationHistory.setCreateTime(new Date());
                operationHistory.save();
                returnSuccessJson();
                return;
            }
        }
        returnFailureJson();
    }

    @ValidateForm
    public void getVisaDetail() {
        int visaId = getParaToInt("visaId");
        Visa visa = Visa.dao.findById(visaId);
        returnSuccessJson(visa);
    }

    @ValidateForm
    public void getVisaChildDetail() {
        int visaChildId = getParaToInt("visaChildId");
        VisaChild visaChild = VisaChild.dao.findById(visaChildId);
        returnSuccessJson(visaChild);
    }

    @ValidateForm
    public void deleteChild() {
        Integer visaChildId = getParaToInt("visaChildId");
        Integer adminId = getSessionAttr("adminId");
        VisaChild visaChild = VisaChild.dao.findById(visaChildId);
        if (visaChild != null && visaChild.getDeleteTime() == null && adminId != null) {
            visaChild.setStatusId(22L);//状态改为已删除
            visaChild.setDeleteTime(new Date());
            if (visaChild.update()) {
                OperationHistory operationHistory = new OperationHistory();
                //更新一条数据
                operationHistory.setTypeId(29L);
                operationHistory.setTargetId((long) visaChild.getId());
                operationHistory.setTableName("visa_child");
                operationHistory.setAdminId(adminId);
                operationHistory.setCreateTime(new Date());
                operationHistory.save();
                returnSuccessJson();
                return;
            }
        }
        returnFailureJson();
    }

    @ValidateForm
    public void getVisaChild() {
        Integer visaId = getParaToInt("visaId");
        List<VisaChild> visas = VisaChild.dao.find("SELECT * FROM visa_child WHERE parent_id = ? AND delete_time IS NULL", visaId);
        returnSuccessJson(visas);
    }
}
